﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.Domain.ProductionWorking;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MangoMes.Write.API.Applications.ProductionWrite;
using MediatR;
using MySql.Data.MySqlClient;

namespace MangoMes.Write.API.Applications.Strippings
{
    public class PerformStripListHandler : IRequestHandler<PerformStripListCommand, ResultsDTO>
    {
        private readonly IBateRepository<Stripping> _stripingRepository;
        private readonly IBateRepository<Rejections> rejectionsRepository;
        private readonly IBateRepository<Stockremoval> _stockremovalRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stripingRepository"></param>
        /// <param name="rejectionsRepository"></param>
        /// <param name="stockremovalRepository"></param>
        public PerformStripListHandler(IBateRepository<Stripping> stripingRepository, IBateRepository<Rejections> rejectionsRepository, IBateRepository<Stockremoval> stockremovalRepository)
        {
            _stripingRepository = stripingRepository;
            this.rejectionsRepository = rejectionsRepository;
            _stockremovalRepository = stockremovalRepository;
        }
        /// <summary>
        /// 执行退料
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(PerformStripListCommand request, CancellationToken cancellationToken)
        {
            DapperHelper dapperHelper = new DapperHelper();
            ResultsDTO res = new ResultsDTO { Code = 0 };
            string dapper = dapperHelper.MangoMesWrite;
            using (MySqlConnection conn = new MySqlConnection(dapperHelper.RBACWrite))
            {
                conn.Open();
                using (MySqlTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        Stripping striping = new Stripping();
                        string proinfo = $"update stripping set Documents=1 where StrippingNode='{ request.StrippingNode} '";
                        res.Code += await _stripingRepository.Add(proinfo, striping, dapper);
                        Rejections reje = new Rejections();
                        string sql = $"select * from rejections where StrippingNode='{request.StrippingNode}' and RejectionStatus=0";
                        var list = rejectionsRepository.GetAll(sql, reje, dapper).ToList();
                        foreach(var item in list)
                        {
                            string sqlUpdate = $"update rejections set RejectionStatus='1'";
                            res.Code += await rejectionsRepository.Add(sqlUpdate,item,dapper);
                            Stockremoval stockremoval = new Stockremoval();
                            string sqlInfo = $"SELECT * FROM stockremoval WHERE productManageNode='{item.productManageNode}'";
                            var info = _stockremovalRepository.GetAll(sqlInfo, stockremoval, dapper).FirstOrDefault();
                            info.StickWorkNum -= item.RejectCount;
                            string sqlUpdateThis = $"UPDATE stockremoval SET StickWorkNum={info.StickWorkNum} WHERE StockWorkId={info.StockWorkId}";
                            res.Code += await _stockremovalRepository.Add(sqlUpdateThis, stockremoval, dapper);
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        res.Code = -1;
                        res.Message = ex.Message;
                    }
                }
            }
            return res;
        }
    }
}
